#coding=utf-8

'''
Created on 2011-9-23

@author: chris
'''
import sys
import os
sys.path.append('/harvester/ripper/source/src/')
from ripper.core.Storage import db

import psutil, time
from ripper.handler.AisexSqliteSync import transfer_data
from ripper.handler.SisSqliteSync import transfer_data as transfer_data_sis

# 运行模式
RUNNING_MODE_DEEP = 'deep' # 深度采集
RUNNING_MODE_SCAN = 'scan' # 平均采集
RUNNING_MODE_HTML = 'html' # 只采集内容不下载

TP_CONTENT = '1'  # 下载内容
TP_FILES = '2'    # 下载文件

ONE_HOUR = 60 * 60
HALF_HOUR = 60 * 30

# 下载遍历排序
FROM_NEW = -1
FROM_OLD = 1

# 创建任务
# 参数: 采集模板名, 动作类型, 运行时间长度(秒) 
def run_task(itemName, type, duration, direction=-1):
    p = psutil.Popen(["/usr/local/bin/python", "Engine.py", itemName, type, str(direction)]) #@UndefinedVariable
    time.sleep(duration)
    p.kill()
    
def run_task2(itemName, duration): # 采集html
    p = psutil.Popen(["/usr/local/bin/python", "Engine.py", itemName, '1', '1', '1']) #@UndefinedVariable
    time.sleep(duration)
    p.kill()

# ---------------  维护方法

# 删除不在aisex数据库中的图片    
def checkAisexTrashFiles(path=None):
    path = '/resource/images_aisex'
    print 'loading image datas...'
    nameList  = []
    num = 0
    q = db.db['data_aisex'].find()
    for data in q:
        if data.has_key('images'):
            for img in data['images']:
                if img['length'] > 0:
                    nameList.append(img['file'])
    print 'total files', len(nameList)
    print 'deleting ...'
    for fname in os.listdir(path):
        if fname not in nameList:
            num = num + 1
            os.remove(os.path.join(path, fname))
                         
    print 'trash num:', num
    
# 删除不在sis数据库中的图片    
def checkSisTrashFiles(path=None):
    path = '/resource/images_sis'
    print 'loading image datas...'
    nameList  = []
    num = 0
    q = db.db['data_sis'].find()
    for data in q:
        if data.has_key('images'):
            for img in data['images']:
                if img['length'] > 0:
                    nameList.append(img['file'])
    print 'total files', len(nameList)
    print 'deleting ...'
    for fname in os.listdir(path):
        if fname not in nameList:
            num = num + 1
            os.remove(os.path.join(path, fname))
                         
    print 'trash num:', num
         
if __name__ == '__main__':
    
    item = 'aisex_pic_asia'
    mode = RUNNING_MODE_SCAN
    if len(sys.argv) > 1:
        mode = sys.argv[1]
        if mode == 'checkAisex':
            checkAisexTrashFiles()
            sys.exit(-1)
        if mode == 'checkSis':
            checkSisTrashFiles()
            sys.exit(-1)
    
    if len(sys.argv) > 2:
        item = sys.argv[2]
        
    
#    item = 'aisex_hentai'
    
#    run_task(item, TP_FILES, ONE_HOUR)
#    run_task(item, TP_CONTENT, ONE_HOUR)
#    run_task(item, TP_CONTENT, ONE_HOUR)
#    run_task(item, TP_FILES, ONE_HOUR)
#    run_task(item, TP_CONTENT, ONE_HOUR)
#    run_task(item, TP_FILES, ONE_HOUR)
    
    
    if mode == RUNNING_MODE_DEEP:
    
        # 单独版面深度采集
        
        run_task(item, TP_CONTENT, HALF_HOUR)
        run_task(item, TP_CONTENT, HALF_HOUR)
        run_task(item, TP_FILES, HALF_HOUR, FROM_OLD)
        run_task(item, TP_FILES, HALF_HOUR, FROM_NEW)
        run_task(item, TP_CONTENT, HALF_HOUR)
        run_task(item, TP_CONTENT, HALF_HOUR)
        run_task(item, TP_FILES, HALF_HOUR, FROM_NEW)
        transfer_data()
    
    elif mode == RUNNING_MODE_SCAN:
    
        # 常规循环任务
        if len(sys.argv) < 3:
            item = 'aisex_asia,aisex_euro,aisex_hentai,aisex_game,aisex_pic_asia,aisex_pic_euro,sis_62,sis_64,sis_68'
        while True:
            run_task(item, TP_CONTENT, 60*30)
            run_task(item, TP_CONTENT, 60*30)
            run_task(item, TP_FILES, HALF_HOUR, FROM_NEW)
            run_task(item, TP_FILES, HALF_HOUR, FROM_OLD)
            transfer_data()
            transfer_data_sis()
            time.sleep(60*60*1)
            
    elif mode == RUNNING_MODE_HTML:
        
            run_task2(item, 60*15)
            run_task2(item, 60*15)
            run_task2(item, 60*15)
            run_task2(item, 60*15)
            run_task2(item, 60*15)
            run_task2(item, 60*15)
            run_task2(item, 60*15)
            run_task2(item, 60*15)

    else :
        
        print 'Error : ', ' RUNNING_MODE not specified  !'
    
    
    
    
    
    
    
    
    